﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Windows.Forms;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;

namespace SenseTimeTracker.CommonSenseControls
{
	public partial class CommonSenseForm : Form
	{
		/// <summary>
		/// Field contains the header control.
		/// </summary>
		[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate",
			Justification = "Reviewed. Suppression is OK here.")]
		protected SenseHeaderControl senseHeaderCtrl;

		/// <summary>
		/// Field contains the list control
		/// </summary>
		[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate",
			Justification = "Reviewed. Suppression is OK here.")]
		protected CommonSenseListControl senseListCtrl;

		public CommonSenseForm()
		{
			InitializeComponent();

			menuItemReady.Text = Resources.MenuItemReady;
			menuItemCancel.Text = Resources.MenuItemCancel;

			// 
			// senseHeaderCtrl
			// 
			senseHeaderCtrl = new StedySoft.SenseSDK.SenseHeaderControl();
			senseHeaderCtrl.Dock = System.Windows.Forms.DockStyle.Top;
			senseHeaderCtrl.Location = new System.Drawing.Point(0, 0);
			senseHeaderCtrl.Name = "timeTrackerHeaderCtrl";
			senseHeaderCtrl.Size = new System.Drawing.Size(240, 25);
			senseHeaderCtrl.TabIndex = 0;
			senseHeaderCtrl.Text = string.Empty;

			// 
			// senseListCtrl
			// 
			senseListCtrl = new CommonSenseListControl();
			senseListCtrl.Dock = System.Windows.Forms.DockStyle.Fill;
			senseListCtrl.FocusedItem = null;
			senseListCtrl.IsSecondaryScrollType = false;
			senseListCtrl.Location = new System.Drawing.Point(0, 25);
			senseListCtrl.Name = "senseListCtrl";
			senseListCtrl.ShowScrollIndicator = true;
			senseListCtrl.Size = new System.Drawing.Size(240, 243);
			senseListCtrl.TabIndex = 1;
			senseListCtrl.TopIndex = 0;
			senseListCtrl.Velocity = 0.55F;

			Controls.Add(senseListCtrl);
			Controls.Add(senseHeaderCtrl);

			Text = "TimeTracker";

			// enable Tap n' Hold & auto SIP for SensePanelTextboxItem(s)
			// SIP.Enable(this.senseListCtrl.Handle);
			inputPanel.EnabledChanged += inputPanelEnabledChanged;
		}

		private int sipOffset = 0;

		private void inputPanelEnabledChanged(object sender, EventArgs e)
		{
			if (inputPanel.Enabled)
			{
				SenseListControl.ISenseListItem item = senseListCtrl.FocusedItem;
				if (item != null)
				{
					Rectangle rectangle = item.ClientRectangle;
					rectangle.Offset(0, senseListCtrl.Bounds.Top);
					if (item is SensePanelTextboxItem)
					{
						if (rectangle.Bottom > inputPanel.VisibleDesktop.Height)
						{
							sipOffset = Math.Abs(inputPanel.VisibleDesktop.Height - rectangle.Bottom);
							senseListCtrl.ScrollList(-sipOffset);
							senseListCtrl.Invalidate();
						}
					}
				}
			}
			else
			{
				if (sipOffset != 0)
				{
					senseListCtrl.ScrollList(sipOffset);
					senseListCtrl.Invalidate();
				}

				sipOffset = 0;
			}
		}

		/// <summary>
		/// Cancels tasks editing
		/// </summary>
		/// <param name="sender">sender object</param>
		/// <param name="e">cancel event arguments</param>
		private void  MenuItemReady_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
		}

		/// <summary>
		/// Finishes tasks editing
		/// </summary>
		/// <param name="sender">sender object</param>
		/// <param name="e">finish event arguments</param>
		private void MenuItemCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}
	}
}